for($chr=1;$chr<24;$chr++)
{
	print "dealing with chr $chr\n";
	#opening the files
	if($chr != 23)
	{
		$file= "ALL_1000G_phase1integrated_feb2012_chr".$chr."_impute.legend.gz";
		open(LEGEND,"gunzip -c /data4/bsi/refdata/genetics/1000Genomes/downloaded_data/release/20110521/impute/ALL_1000G_phase1integrated_feb2012_impute/$file |") or die " no file found $file \n";
		open(WRLEGEND,"| gzip -c > ./impute_ref_maf0_005p_indels_1/$file") or die "unable to write $file\n";
		$file= "ALL_1000G_phase1integrated_feb2012_chr".$chr."_impute.hap.gz";
		open(HAP,"gunzip -c /data4/bsi/refdata/genetics/1000Genomes/downloaded_data/release/20110521/impute/ALL_1000G_phase1integrated_feb2012_impute/$file |") or die " no file found $file \n";
		open(WRHAP,"| gzip -c > ./impute_ref_maf0_005p_indels_1/$file") or die "unable to write $file\n";
		#$file="genetic_map_chrX_nonPAR_combined_b37.txt";
		$file="genetic_map_chr$chr"."_combined_b37.txt";
		open(GEN,"/data4/bsi/refdata/genetics/1000Genomes/downloaded_data/release/20110521/impute/ALL_1000G_phase1integrated_feb2012_impute/$file") or die "no file $file \n";
		open(WRGEN,">./impute_ref_maf0_005p_indels_1/$file") or die "unable to write $file\n";
	}
	else
	{
		$file = "ALL_1000G_phase1integrated_feb2012_chrX_nonPAR_impute.legend.gz";
		open(LEGEND,"gunzip -c /data4/bsi/refdata/genetics/1000Genomes/downloaded_data/release/20110521/impute/ALL_1000G_phase1integrated_feb2012_impute/$file |") or die " no file found $file \n";
		open(WRLEGEND,"| gzip -c > ./impute_ref_maf0_005p_indels_1/$file") or die "unable to write $file\n";
		$file= "ALL_1000G_phase1integrated_feb2012_chrX_nonPAR_impute.hap.gz";
		open(HAP,"gunzip -c /data4/bsi/refdata/genetics/1000Genomes/downloaded_data/release/20110521/impute/ALL_1000G_phase1integrated_feb2012_impute/$file |") or die " no file found $file \n";
		open(WRHAP,"| gzip -c > ./impute_ref_maf0_005p_indels_1/$file") or die "unable to write $file\n";
		#$file="genetic_map_chr$chr"."_combined_b37.txt";
		$file="genetic_map_chrX_nonPAR_combined_b37.txt";
		#print $file."\n";
		open(GEN,"/data4/bsi/refdata/genetics/1000Genomes/downloaded_data/release/20110521/impute/ALL_1000G_phase1integrated_feb2012_impute/$file") or die "no file $file \n"; 
		open(WRGEN,">./impute_ref_maf0_005p_indels_1/$file") or die "unable to write $file\n";
	}
	#next;
	$line = <LEGEND>;
	print WRLEGEND $line;
	$line=<GEN>;
	print WRGEN $line;
	undef(%pos);
	while(<LEGEND>)
	{
		$line = <HAP>;
		@legend = split(" ",$_);
		$count_1 =($line =~ tr/1//);
		$count_0 = ($line =~ tr/0//);
		if($chr == 23)
		{
			$count_d = ($line =~ tr/-//);
			if($count_1+$count_0+$count_d != 2184)
			{
				die "count_1 $count_1 count_0 $count_0 count_d $count_d\n $line";
			}
		}
		else
		{
			if($count_1+$count_0 != 2184)
                        {
                                die "count_1 $count_1 count_0 $count_0  $line\n";
                        }
		}	
		$pos{$legend[1]}=1;
		#print "count_1 $count_1 count_0 $count_0\n";
		$percent_0 = $count_0/($count_1+$count_0);
		$percent_1 = $count_1/($count_1+$count_0);
		#if($percent_0 > 0.005 && $percent_1 > 0.005 )
		if($percent_0 > 0.005 && $percent_1 > 0.005 && ($legend[4] eq "SNP" || $legend[4] eq "INDEL"))
		{
			print WRLEGEND $_;
			print WRHAP $line;
		}
	}
	#die;
	while(<GEN>)
	{
		chomp($_);
		@gen=split(" ",$_);
		if(exists($pos{$gen[0]}))
		{
			print WRGEN $_."\n";
		}
	}
}
